package com.wondersgroup.ui;

import com.fr.base.BaseUtils;
import com.fr.base.FRContext;
import com.fr.base.Parameter;
import com.fr.design.actions.UpdateAction;
import com.fr.design.data.datapane.preview.PreviewTablePane;
import com.fr.design.data.tabledata.tabledatapane.AbstractTableDataPane;
import com.fr.design.editor.ValueEditorPane;
import com.fr.design.editor.ValueEditorPaneFactory;
import com.fr.design.editor.editor.*;
import com.fr.design.gui.frpane.ObjectProperiesPane;
import com.fr.design.gui.ibutton.UIPasswordField;
import com.fr.design.gui.ilable.UILabel;
import com.fr.design.gui.itextfield.UITextField;
import com.fr.design.gui.itoolbar.UIToolbar;
import com.fr.design.layout.FRGUIPaneFactory;
import com.fr.design.menu.ToolBarDef;
import com.fr.design.scrollruler.ModLineBorder;
import com.fr.design.utils.gui.GUICoreUtils;
import com.fr.general.Inter;
import com.fr.stable.ListMap;
import com.wondersgroup.model.ParQueryTableData;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;

/**
 * Created by richie on 2016/10/8.
 */
public class QueryTableDataPane extends AbstractTableDataPane<ParQueryTableData> {
    /**
     * 定义界面
     */
    //预览按钮
    private static final String PREVIEW_BUTTON = Inter.getLocText("Preview");

    private ValueEditorPane documentIDEditor;
    private ValueEditorPane templateCodeEditor;
    private UITextField dataTextField;
    private UITextField poorTextField;
    private UITextField userTextField;
    private UIPasswordField pwTextField;
    private UITextField dataNameTextField;

    public QueryTableDataPane() {
        this.setLayout(FRGUIPaneFactory.createBorderLayout());
        dataTextField = new UITextField(15);
        poorTextField = new UITextField(10);
        userTextField = new UITextField(15);
        dataNameTextField = new UITextField(10);
        pwTextField = new UIPasswordField();
        pwTextField.setColumns(15);

        JPanel allPane = FRGUIPaneFactory.createY_AXISBoxInnerContainer_S_Pane();

        /**
         * 数据库部分的信息面板
         */
        JPanel headPane = FRGUIPaneFactory.createY_AXISBoxInnerContainer_S_Pane();
        JPanel dataPane = GUICoreUtils.createFlowPane(new Component[]{new UILabel("数据库地址:"), dataTextField},
                FlowLayout.LEFT, 2, 2);
        JPanel poorPane = GUICoreUtils.createFlowPane(new Component[]{new UILabel("数据库端口:"), poorTextField},
                FlowLayout.LEFT, 2, 2);
        JPanel dataNamePane = GUICoreUtils.createFlowPane(new Component[]{new UILabel("数据库名:"), dataNameTextField},
                FlowLayout.LEFT, 2, 2);
        JPanel userPane = GUICoreUtils.createFlowPane(new Component[]{new UILabel("用户名:"), userTextField},
                FlowLayout.LEFT, 2, 2);
        JPanel pwPane = GUICoreUtils.createFlowPane(new Component[]{new UILabel("密码:"), pwTextField},
                FlowLayout.LEFT, 2, 2);
        headPane.add(dataPane);
        headPane.add(poorPane);
        headPane.add(dataNamePane);
        headPane.add(userPane);
        headPane.add(pwPane);
        headPane.setBorder(BorderFactory.createTitledBorder(new ModLineBorder(ModLineBorder.TOP), "数据库信息:"));
        /**
         * 填报部分的信息面板
         */
        JPanel headPane2 = FRGUIPaneFactory.createY_AXISBoxInnerContainer_S_Pane();
        UILabel documentLB = new UILabel("documentID");
        UILabel templateLB = new UILabel("TemplateCode");
        FormulaEditor formulaEditor = new FormulaEditor(Inter.getLocText("Parameter-Formula"));
        formulaEditor.setEnabled(true);
        documentIDEditor = ValueEditorPaneFactory.createValueEditorPane(new Editor[]{new TextEditor(), new ParameterEditor(), formulaEditor});
        templateCodeEditor = ValueEditorPaneFactory.createValueEditorPane(new Editor[]{new TextEditor(), new ParameterEditor(), new FormulaEditor(Inter.getLocText("Parameter-Formula"))});
        JPanel p = GUICoreUtils.createFlowPane(documentLB,documentIDEditor);
        JPanel q = GUICoreUtils.createFlowPane(templateLB,templateCodeEditor);
        headPane2.add(p);
        headPane2.add(q);
        headPane2.setBorder(BorderFactory.createTitledBorder(new ModLineBorder(ModLineBorder.TOP), "文档信息:"));
        allPane.add(headPane);
        allPane.add(headPane2);
        this.add(createToolBar(), BorderLayout.NORTH);
        this.add(allPane, BorderLayout.CENTER);
    }

    /**
     * 工具条
     *
     * @return
     */
    private JToolBar createToolBar() {
        ToolBarDef toolBarDef = new ToolBarDef();
        toolBarDef.addShortCut(new PreviewQueryTableDataAction());
        UIToolbar editToolBar = ToolBarDef.createJToolBar();
        toolBarDef.updateToolBar(editToolBar);
        return editToolBar;
    }

    private class PreviewQueryTableDataAction extends UpdateAction {
        public PreviewQueryTableDataAction() {
            this.setName(PREVIEW_BUTTON);
            this.setMnemonic('P');
            this.setSmallIcon(BaseUtils.readIcon("/com/fr/design/images/m_file/preview.png"));
        }

        public void actionPerformed(ActionEvent evt) {
            PreviewTablePane.previewTableData(QueryTableDataPane.this.updateBean());
        }
    }

    /**
     * 覆写方法
     */
    @Override
    public void populateBean(ParQueryTableData ob) {
        if (ob == null) {
            ParQueryTableData parQueryTableData = emptyListMap();
            documentIDEditor.populate(parQueryTableData.getDocumentID());
            templateCodeEditor.populate(parQueryTableData.getTemplateCode());
            dataTextField.setText(parQueryTableData.getDataAddress());
            poorTextField.setText(parQueryTableData.getDatapoor());
            userTextField.setText(parQueryTableData.getUserName());
            pwTextField.setText(parQueryTableData.getPassWord());
            dataNameTextField.setText(parQueryTableData.getDataName());

        } else {
            documentIDEditor.populate(ob.getDocumentID());
            templateCodeEditor.populate(ob.getTemplateCode());
            dataTextField.setText(ob.getDataAddress());
            poorTextField.setText(ob.getDatapoor());
            userTextField.setText(ob.getUserName());
            pwTextField.setText(ob.getPassWord());
            dataNameTextField.setText(ob.getDataName());
        }
    }

    @Override
    public ParQueryTableData updateBean() {
        ParQueryTableData parQueryTableData = new ParQueryTableData();
        String documentID = documentIDEditor.getCurrentEditor().getValue().toString();
        String templateCode = templateCodeEditor.getCurrentEditor().getValue().toString();
        String dataAddress = dataTextField.getText();
        String dataPoor = poorTextField.getText();
        String dataName = dataNameTextField.getText();
        String userName = userTextField.getText();
        String passWord = String.valueOf(pwTextField.getPassword());
        String url = "jdbc:sqlserver://" + dataAddress + ":" + dataPoor + ";databaseName=" + dataName;

        FRContext.getLogger().log(Level.INFO, "界面信息：{DocumentID：**" + documentID + "---templateCode：**" + templateCode + "---url:**" + url + "---User：**" + userName + "---PassWord:**" + passWord);
        parQueryTableData.setDocumentID(documentID);
        parQueryTableData.setTemplateCode(templateCode);
        parQueryTableData.setUserName(userName);
        parQueryTableData.setPassWord(passWord);
        parQueryTableData.setDataAddress(dataAddress);
        parQueryTableData.setDatapoor(dataPoor);
        parQueryTableData.setDataName(dataName);
        return parQueryTableData;
    }

    public ParQueryTableData emptyListMap() {

        ParQueryTableData parQueryTableData = new ParQueryTableData();
        parQueryTableData.setDocumentID("documentID");
        parQueryTableData.setTemplateCode("templateCode");
        parQueryTableData.setDataAddress("192.168.0.60");
        parQueryTableData.setDatapoor("1433");
        parQueryTableData.setDataName("nurse");

        return parQueryTableData;
    }

    @Override
    protected String title4PopupWindow() {
        return "Item数据查询";
    }
}
